library(haven)
# setwd()

impdat19_bw <- read_spss("./Data/Race_IAT.public.2019.sav")
# Subset to USA
impdat19_bw <- subset(impdat19_bw, countryres_num == 1)

# Meta Info ---------------------------------------------------------------
impdat19_bw$date_mdy <- format(impdat19_bw$date, "%Y-%m-%d")
impdat19_bw$date_mdy <- as.Date(impdat19_bw$date_mdy)
impdat19_bw$week <- format(as.Date(impdat19_bw$date), "%W")

# Demographics ---------------------------------------------------------------
# Age
impdat19_bw$age[which(impdat19_bw$birthyear > 1900)] <- impdat19_bw$year[which(impdat19_bw$birthyear > 1900)] - impdat19_bw$birthyear[which(impdat19_bw$birthyear > 1900)]
impdat19_bw$age_sc <- (impdat19_bw$age-min(impdat19_bw$age, na.rm = T))/(max(impdat19_bw$age, na.rm = T)-min(impdat19_bw$age, na.rm = T))

impdat19_bw$age_cat4 <- NA
impdat19_bw$age_cat4[which(impdat19_bw$age >= 18 & impdat19_bw$age <= 29)] <- "18-29"
impdat19_bw$age_cat4[which(impdat19_bw$age >= 30 & impdat19_bw$age <= 44)] <- "30-44"
impdat19_bw$age_cat4[which(impdat19_bw$age >= 45 & impdat19_bw$age <= 64)] <- "45-64"
impdat19_bw$age_cat4[which(impdat19_bw$age >= 65)] <- "65+"
impdat19_bw$age_cat4 <- as.factor(impdat19_bw$age_cat4)

# Sex
impdat19_bw$sex <- NA
impdat19_bw$sex[which(impdat19_bw$birthSex == 2)] <- "f"
impdat19_bw$sex[which(impdat19_bw$birthSex == 1)] <- "m"

# Race and Ethnicity
impdat19_bw$race <- factor(impdat19_bw$raceomb002, 
                           levels = c(1:max(impdat19_bw$raceomb002, na.rm = T)),
                           labels = c("American Indian/Alaskan Native",
                                      "East Asian",
                                      "South Asian",
                                      "Native Hawaiian/Pacific Islander",
                                      "Black",
                                      "White",
                                      "Other", 
                                      "Multiracial"))

impdat19_bw$hisp <- NA
impdat19_bw$hisp[which(impdat19_bw$ethnicityomb == 1)] <- 1
impdat19_bw$hisp[which(impdat19_bw$ethnicityomb == 2)] <- 0 # drops unknowns

impdat19_bw$race5 <- NA
impdat19_bw$race5[which(impdat19_bw$race %in% c("East Asian", "South Asian", "Native Hawaiian/Pacific Islander") & impdat19_bw$hisp == 0)] <- "Asian/Pacific Islander"
impdat19_bw$race5[which(impdat19_bw$race == "Black" & impdat19_bw$hisp == 0)] <- "Black"
impdat19_bw$race5[which(impdat19_bw$race == "White" & impdat19_bw$hisp == 0)] <- "White"
impdat19_bw$race5[which(impdat19_bw$race %in% c("Other", "Multiracial") & impdat19_bw$hisp == 0)] <- "Other"
impdat19_bw$race5[which(impdat19_bw$hisp == 1)] <- "Hispanic"
impdat19_bw$race5 <- as.factor(impdat19_bw$race5)
impdat19_bw$race5 <- factor(impdat19_bw$race5, levels(impdat19_bw$race5)[c(1:3,5,4)])

impdat19_bw$race3 <- NA
impdat19_bw$race3[which(impdat19_bw$race5 == "White")] <- "White"
impdat19_bw$race3[which(impdat19_bw$race5 == "Black")] <- "Black"
impdat19_bw$race3[which(impdat19_bw$race5 != "Black" & impdat19_bw$race5 != "White")] <- "Other"

# Ideology
impdat19_bw$ideo3 <- NA
impdat19_bw$ideo3[which(impdat19_bw$politicalid_7 < 4)] <- 1
impdat19_bw$ideo3[which(impdat19_bw$politicalid_7 == 4)] <- 0
impdat19_bw$ideo3[which(impdat19_bw$politicalid_7 > 4)] <- -1
impdat19_bw$ideo3_lab <- factor(impdat19_bw$ideo3,
                                labels = c("liberal", "moderate", "conservative"))

impdat19_bw$ideo_lib <- ifelse(impdat19_bw$ideo3 == -1, 1, 0)
impdat19_bw$ideo_mod <- ifelse(impdat19_bw$ideo3 == 0, 1, 0)
impdat19_bw$ideo_con <- ifelse(impdat19_bw$ideo3 == 1, 1, 0)

impdat19_bw$ideo7 <- impdat19_bw$politicalid_7*-1 + 7
impdat19_bw$ideo7_sc <- impdat19_bw$ideo7/6

# Region
impdat19_bw$south <- NA
impdat19_bw$south <- ifelse(impdat19_bw$STATE %in% c("VA", "NC", "SC", "GA", "FL", "MS", "AL", "TN", "AR", "LA", "TX"),
                            1, 0)
impdat19_bw$south[impdat19_bw$STATE == ""] <- NA

impdat19_bw$census_region <- NA
impdat19_bw$census_region[which(impdat19_bw$STATE %in% c("DE", "MD", "DC", "WV", "KY", "OK",
                                                         "VA", "NC", "SC", "GA", "FL", "MS", "AL", 
                                                         "TN", "AR", "LA", "TX"))] <- "South"
impdat19_bw$census_region[which(impdat19_bw$STATE %in% c("CA", "OR", "WA", "MT", "WY",
                                                         "ID", "CO", "UT", "AZ", "NM",
                                                         "NV", "AK", "HI"))] <- "West"
impdat19_bw$census_region[which(impdat19_bw$STATE %in% c("ND", "SD", "MN", "WI",
                                                         "IA", "NE", "KS", "MO",
                                                         "IL", "IN", "MI", "OH"))] <- "Midwest"
impdat19_bw$census_region[which(impdat19_bw$STATE %in% c("ME", "VT", "NH", "MA", "RI",
                                                         "CT", "NY", "NJ", "PA"))] <- "Northeast"
impdat19_bw$census_region[impdat19_bw$STATE == ""] <- NA
impdat19_bw$census_region <- as.factor(impdat19_bw$census_region)


# Education
impdat19_bw$edu_cat <- NA
impdat19_bw$edu_cat[which(impdat19_bw$edu <= 3)] <- 1 # Less than HS
impdat19_bw$edu_cat[which(impdat19_bw$edu == 4)] <- 2 # HS 
impdat19_bw$edu_cat[which(impdat19_bw$edu == 5 | impdat19_bw$edu == 6)] <- 3 # Some college or associates
impdat19_bw$edu_cat[which(impdat19_bw$edu == 7)] <- 4 # College
impdat19_bw$edu_cat[which(impdat19_bw$edu >= 8)] <- 5 # Adv degree or schooling
impdat19_bw$col <- ifelse(impdat19_bw$edu_cat >= 4, 1, 0)

impdat19_bw$edu_cat4 <- NA
impdat19_bw$edu_cat4[which(impdat19_bw$edu_cat <= 2)] <- "HS or Less"
impdat19_bw$edu_cat4[which(impdat19_bw$edu_cat == 3)] <- "Some College"
impdat19_bw$edu_cat4[which(impdat19_bw$edu_cat == 4)] <- "College"
impdat19_bw$edu_cat4[which(impdat19_bw$edu_cat == 5)] <- "Post-Grad"
impdat19_bw$edu_cat4 <- factor(impdat19_bw$edu_cat4,
                               levels = c("HS or Less",
                                          "Some College",
                                          "College",
                                          "Post-Grad"))

# Reason for Visiting
# impdat19_bw$broughtwebsite
impdat19_bw$broughtwebsite2 <- NA
impdat19_bw$broughtwebsite2[which(impdat19_bw$broughtwebsite %in% c("Assignment for school", "Assignment for work"))] <- "Assignment school/work"
impdat19_bw$broughtwebsite2[which(impdat19_bw$broughtwebsite %in% c("Mention in a news story (any medium)"))] <- "News Mention"
impdat19_bw$broughtwebsite2[which(impdat19_bw$broughtwebsite %in% c("My Internet search for this topic or a related topic", "Mention or link at a non-news Internet site"))] <- "Internet Mention/Search"
impdat19_bw$broughtwebsite2[which(impdat19_bw$broughtwebsite %in% "Recommendation of a friend or co-worker")] <- "Peer Recommendation"
impdat19_bw$broughtwebsite2[which(impdat19_bw$broughtwebsite %in% c("Other", "null"))] <- "Other/null"

impdat19_bw$broughtwebsite2 <- as.factor(impdat19_bw$broughtwebsite2)

# Interview Date Indicators -----------------------------------------------
impdat19_bw$post_gf <- ifelse(impdat19_bw$date_mdy > "2020-05-25", 1, 0)
impdat19_bw$post_gf_1month <- ifelse(impdat19_bw$date_mdy > "2020-05-25" & impdat19_bw$date_mdy <= "2020-06-26", 
                                     1, 0)
impdat19_bw$post_gf_2month <- ifelse(impdat19_bw$date_mdy > "2020-06-26" & impdat19_bw$date_mdy <= "2020-07-26", 
                                     1, 0)
impdat19_bw$post_gf_3month <- ifelse(impdat19_bw$date_mdy > "2020-07-26" & impdat19_bw$date_mdy <= "2020-08-26", 
                                     1, 0)


# Attitudes ---------------------------------------------------------------
# Thermometers
impdat19_bw$tblack_sc <- impdat19_bw$Tblack_0to10/10
impdat19_bw$twhite_sc <- impdat19_bw$Twhite_0to10/10

impdat19_bw$t_wb <- (impdat19_bw$twhite_sc - impdat19_bw$tblack_sc)

# D Score
# Per email communication
impdat19_bw$IAT_D <- NA
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all < -.65)] <- "Strong Black"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all >= -.65 & impdat19_bw$D_biep.White_Good_all <= -.35)] <- "Moderate Black"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all > -.35 & impdat19_bw$D_biep.White_Good_all <= -.15)] <- "Slight Black"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all > -.15 & impdat19_bw$D_biep.White_Good_all < 0)] <- "Little or No Black"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all >= 0 & impdat19_bw$D_biep.White_Good_all < .15)] <- "Little or No White"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all >= .15 & impdat19_bw$D_biep.White_Good_all < .35)] <- "Slight White"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all >= .35 & impdat19_bw$D_biep.White_Good_all <= .65)] <- "Moderate White"
impdat19_bw$IAT_D[which(impdat19_bw$D_biep.White_Good_all > .65)] <- "Strong White"
impdat19_bw$IAT_D <- factor(impdat19_bw$IAT_D,
                            levels = c("Strong Black", "Medium Black", "Slight Black",
                                       "Little or No Black", "Little or No White",
                                       "Slight White", "Medium White", "Strong White"))


